﻿using System.IO;
using KillBill.Common.Security;

namespace KillBill.Installers.DataInstaller
{
    public class ScriptGenerator
    {
        public void GenerateScript (string outPath)
        {
            StringWriter stringWriter;
            string sContent = File.ReadAllText(@"BaseScript.sql");
            using (stringWriter = new StringWriter())
            {
                stringWriter.Write(sContent);
                stringWriter.WriteLine("");
                InsertRoles(ref stringWriter);

                InsertDefaultUser(ref stringWriter);

                InsertProgramParameters(ref stringWriter);
            }
            File.WriteAllText(outPath,stringWriter.ToString());
        }

        public void InsertRoles(ref StringWriter stringWriter)
        {
            stringWriter.WriteLine("INSERT INTO EmployeeRole VALUES ('Administrador', 'Administrador')");
            stringWriter.WriteLine("go");
            stringWriter.WriteLine("INSERT INTO EmployeeRole VALUES ('Cajero', 'Cajero')");
            stringWriter.WriteLine("go");
            stringWriter.WriteLine("INSERT INTO EmployeeRole VALUES ('Bodeguero', 'Bodeguero')");
            stringWriter.WriteLine("go");
        }
        public void InsertDefaultUser(ref StringWriter stringWriter)
        {
            stringWriter.WriteLine("INSERT INTO Person VALUES ('" +
                                   InitialData.UserId + "', '" +
                                   InitialData.FirstName + "', '" +
                                   InitialData.LastName + "', '" +
                                   InitialData.Email + "','" +
                                   InitialData.Address + "','" +
                                   InitialData.Phone + "', " +
                                   "GETDATE())"
                );
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Employee VALUES('" +
                InitialData.UserId + "', '" +
                "Administrador' , '" + 
                Encryption.EncryptSha256("Administrador") +"' , 1)");
            stringWriter.WriteLine("go");
        }
        public void InsertProgramParameters(ref StringWriter stringWriter)
        {
            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'COMPANY' , 'COMPANY', '" + InitialData.CompanyName +"')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'COMPANY_ADDRESS' , 'COMPANY_ADDRESS', '" + InitialData.CompanyAddress + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'COMPANY_PHONE' , 'COMPANY_PHONE', '" + InitialData.CompanyContactNumber + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'COMPANY_MAIL' , 'COMPANY_MAIL', '" + InitialData.CompanyContactMail + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'MAIL_SERVER' , 'MAIL_SERVER', '" + InitialData.MailServer + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'AUTO_ORDER_MAIL' , 'AUTO_ORDER_MAIL', '" + InitialData.AutomaticEmail + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'AUTO_MAIL_USER' , 'AUTO_MAIL_USER', '" + InitialData.AutomaticMailUserName + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'AUTO_MAIL_PASS' , 'AUTO_MAIL_PASS', '" + InitialData.AutomaticMailPassWord + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'ORDER_CONTACT_PHONE' , 'ORDER_CONTACT_PHONE', '" + InitialData.OrderManagerPhone + "')");
            stringWriter.WriteLine("go");

            stringWriter.WriteLine("INSERT INTO Parameter VALUES ( 'ORDER_CONTACT_MAIL' , 'ORDER_CONTACT_MAIL', '" + InitialData.OrderManagerMail + "')");
            stringWriter.WriteLine("go");
        }
    }
}
